#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);
    gl = new PAINEL_OPEN_GL(this);
    ui->gridLayout->addWidget(gl, 0, 0, 4, 1);
    gl->setSize(this->width(),this->height());
    QDir dir;
    QString path2 = dir.absolutePath() + "/DOGALERT.fdb";
    update();
}

Widget::~Widget()
{
    delete gl;
    delete ui;
}

void Widget::filllScenary()
{
    gl->scenary.qtdePlanesX =   ui->nudEixoXPlanes->value();
    gl->scenary.qtdePlanesZ =   ui->nudEixoZPlanes->value();
    gl->scenary.distPlanesX =   ui->nudDistX->value();
    gl->scenary.distPlanesZ =   ui->nudDistZ->value();
    if(ui->optGoraudShaded->isChecked())
        gl->scenary.modeView    =   gl->scenary.GouraudShaded;
    else if(ui->optWireFrame->isChecked())
        gl->scenary.modeView    =   gl->scenary.Wireframe;
    else if(ui->optTexturized->isChecked())
        gl->scenary.modeView    =   gl->scenary.Texturized;
    else if(ui->optPoints->isChecked())
        gl->scenary.modeView    =   gl->scenary.Points;
    gl->scenary.origem.x    =   ui->nudOrigemX->value();
    gl->scenary.origem.y    =   ui->nudOrigemY->value();
    gl->scenary.origem.z    =   ui->nudOrigemZ->value();
    gl->scenary.leftHanded  =   ui->chkLeftHanded->checkState();
    gl->scenary.load();
}
void Widget::update()
{
    filllScenary();
    gl->render();
}

void Widget::on_optWireFrame_clicked()
{
    update();
}

void Widget::on_optGoraudShaded_clicked()
{
    update();
}

void Widget::on_optTexturized_clicked()
{
    update();
}

void Widget::on_nudEixoXPlanes_valueChanged(int i)
{
    ui->nduPisoDe->setMaximum(i * ui->nudEixoZPlanes->value());
    ui->nduPisoAte->setMaximum(i * ui->nudEixoZPlanes->value());
    if(ui->nduPisoAte->value() > ui->nduPisoDe->value())
        ui->nduPisoAte->setValue(ui->nduPisoDe->value());
    update();
}

void Widget::on_nudEixoZPlanes_valueChanged(int i)
{
    ui->nduPisoDe->setMaximum(i * ui->nudEixoXPlanes->value());
    ui->nduPisoAte->setMaximum(i * ui->nudEixoXPlanes->value());
    if(ui->nduPisoAte->value() > ui->nduPisoDe->value())
        ui->nduPisoAte->setValue(ui->nduPisoDe->value());
    update();
}

void Widget::on_nudDistX_valueChanged(double )
{
    update();
}

void Widget::on_nudDistZ_valueChanged(double )
{
    update();
}

void Widget::on_nudOrigemX_valueChanged(double )
{
    update();
}

void Widget::on_nudOrigemY_valueChanged(double )
{
    update();
}

void Widget::on_nudOrigemZ_valueChanged(double )
{
    update();
}

void Widget::on_optPoints_clicked()
{
    update();
}

void Widget::on_chkLeftHanded_clicked()
{
    update();
}

void Widget::on_btnRandomizar_clicked()
{
    unsigned int i = ui->nduPisoDe->value() - 1;
    unsigned int j = ui->nduPisoAte->value() - 1;
    while(i <= j)
    {
        gl->scenary.ramdomizeScenary(ui->nudAlturaSubtrae->value(),ui->nudAlturaSoma->value(),i);
        i++;
    }
    update();
}

void Widget::on_nduPisoDe_valueChanged(int i)
{
    if(i > ui->nduPisoAte->value())
        ui->nduPisoAte->setValue(i);
}

void Widget::on_nduPisoAte_valueChanged(int i)
{
    if(ui->nduPisoDe->value() > i)
        ui->nduPisoDe->setValue(i);
}

void Widget::on_btnOpenTexture_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Abrir arquivo"),
                                                    "",
                                                    tr("Images (*.png *.bmp *.jpg)"));
    if(!fileName.isEmpty())
    {
        QStringList n = fileName.split("/");
        if(gl->scenary.loadTextures(fileName,n[n.count()-1]))
        {
            ui->cmbTextura->addItem(n[n.count()-1]);
            ui->optTexturized->setChecked(true);
            gl->render();
            ui->cmbTextura->setCurrentIndex(ui->cmbTextura->count()-1);
        }
    }
}

void Widget::on_cmbTextura_currentIndexChanged(QString textureName)
{
    gl->setTexture(textureName);
    gl->render();
}

void Widget::on_nudRed_valueChanged(double )
{
    gl->scenary.setColorMaskTexture(ui->nudRed->value(),ui->nudGreen->value(),ui->nudBlue->value());
    gl->render();
}

void Widget::on_nudGreen_valueChanged(double )
{
    gl->scenary.setColorMaskTexture(ui->nudRed->value(),ui->nudGreen->value(),ui->nudBlue->value());
    gl->render();
}

void Widget::on_nudBlue_valueChanged(double )
{
    gl->scenary.setColorMaskTexture(ui->nudRed->value(),ui->nudGreen->value(),ui->nudBlue->value());
    gl->render();
}
